//
//  CSNavigationBottomBannerViewController.swift
//  Cycling
//
//  Created by CSS on 3/30/23.
//

import UIKit

import MapboxNavigation
import MapboxCoreNavigation
import MapboxDirections

class CSNavigationBottomBannerViewController: BaseViewController {
    
    @IBOutlet private weak var shapeContentView: UIView!
    @IBOutlet private weak var commandSignView: UIImageView!
    @IBOutlet private weak var commandNameLbl: UILabel!
    @IBOutlet private weak var commandRemainingLbl: UILabel!
    @IBOutlet private weak var pauseButton: UIButton!
    
    public var navigationService: CSNavigationService?
    public var bannerActionTypeBlock:((Int) -> Void)?
    
    lazy private var distanceFormatter: DistanceFormatter = .init()
    private var isNavigationRunning = true {
        didSet {
            reloadPauseButtonStatus()
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .clear
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        
        shapeContentView.layoutIfNeeded()
        shapeContentView.cornerShaper(withRoundingCorners: [.topLeft, .topRight], radium: 26)
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        view.isUserInteractionEnabled = true
    }
    
    // MARK: 事件处理方法
    @IBAction private func stopButtonAction(_ sender: Any) {
        navigationService?.endNavigation()
        bannerActionTypeBlock?(0)
    }
    
    @IBAction private func pauseButtonAction(_ sender: Any) {
        if isNavigationRunning {
            navigationService?.stopService() { [weak self] in
                if $0 {
                    self?.isNavigationRunning = false
                    self?.bannerActionTypeBlock?(1)
                }
            }
        } else {
            navigationService?.startService() { [weak self] in
                if $0 {
                    self?.isNavigationRunning = true
                    self?.bannerActionTypeBlock?(1)
                }
            }
        }
    }
    
}

// MARK: 导航委托
extension CSNavigationBottomBannerViewController: NavigationComponent {
    
    func navigationService(_ service: NavigationService, didUpdate progress: RouteProgress, with location: CLLocation, rawLocation: CLLocation) {
        
        var distance = progress.currentLegProgress.currentStepProgress.distanceRemaining
        distance = distance > 5 ? distance : 0
        commandRemainingLbl.text = distanceFormatter.string(from: distance)
        
    }
    
    func navigationService(_ service: NavigationService, didPassVisualInstructionPoint instruction: VisualInstructionBanner, routeProgress: RouteProgress) {
        DispatchQueue.main.async { [weak self] in
            guard let self = self else {
                return
            }
            self.reloadManeuverValue(withInstruction: instruction.primaryInstruction)
        }
        
    }
    
    func navigationService(_ service: NavigationService, didPassSpokenInstructionPoint instruction: SpokenInstruction, routeProgress: RouteProgress) {
        
    }
    
}

// MARK: 工具类方法
private extension CSNavigationBottomBannerViewController {
    
    func reloadManeuverValue(withInstruction instruction: VisualInstruction?) {
        guard let instruction = instruction else {
            return
        }
        let direction = instruction.maneuverDirection ?? .straightAhead
        switch direction {
        case .left, .sharpLeft, .slightLeft:
            commandSignView.image = .init(named: "map_navigation_turn_left")
            commandNameLbl.text = "左转"
        case .right, .sharpRight, .slightRight:
            commandSignView.image = .init(named: "map_navigation_turn_right")
            commandNameLbl.text = "右转"
        default:
            commandSignView.image = .init(named: "map_navigation_go_traight")
            commandNameLbl.text = "直行"
        }
    }
    
    func reloadPauseButtonStatus() {
        if isNavigationRunning {
            pauseButton.setImage(.init(named: "map_navigation_pause"), for: .normal)
        } else {
            pauseButton.setImage(.init(named: "map_navigation_continue"), for: .normal)
        }
    }
    
}
